package com.wisdytech.linkdcs.ieMaintenance.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.wisdytech.common.exception.ServiceException;
import com.wisdytech.linkdcs.ieMaintenance.model.IeMaintenance;
import com.wisdytech.linkdcs.ieMaintenance.model.TmMaterial;
import com.github.pagehelper.PageInfo;
import com.wisdytech.common.bean.Filter;
import com.wisdytech.common.constants.CommonConstants;
import com.wisdytech.common.handler.BaseService;
import com.wisdytech.common.utils.BaseUtil;
import com.wisdytech.linkdcs.equipment.dao.IEqEquipmentDao;
import com.wisdytech.linkdcs.equipment.model.Equipment;
import com.wisdytech.linkdcs.ieMaintenance.dao.IeMaintenanceDao;
import com.wisdytech.linkdcs.ieMaintenance.service.IIeMaintenanceService;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chenbin
 * @since 2018-07-20
 */
@Service("iIeMaintenanceService")
@Transactional
public class IeMaintenanceServiceImpl extends BaseService<IeMaintenanceDao, IeMaintenance> implements IIeMaintenanceService {

	@Resource
	private IeMaintenanceDao ieMaintenanceDao;
	@Override
	public PageInfo<TmMaterial> searchProductByProductCode(Filter filter, String productCode) {
        List<TmMaterial> list =
                super.beforeList(IeMaintenanceDao.class, filter).searchProductByProductCode(productCode);
		return  new PageInfo<TmMaterial>(list);
	}
	@Override
	public TmMaterial getProductUnitbyPkId(String pkId) {
		if(pkId.isEmpty()) {
			return new TmMaterial();
		}
		return ieMaintenanceDao.getProductUnitbyPkId(pkId);
	}
	@Override
	public void addIEMaintenance(IeMaintenance ieMaintenance, String loginName) {
        EntityWrapper<IeMaintenance> ew = new EntityWrapper();
        ew.eq("EQUIP_CODE",ieMaintenance.getEquipCode());
        ew.eq("PRODUCT_CODE",ieMaintenance.getProductCode());
        ew.eq("DEL_FLAG",CommonConstants.DEL_FLAG);
        List<IeMaintenance> list = ieMaintenanceDao.selectList(ew);
        if(!list.isEmpty()){
            throw new ServiceException("同一个设备，同一产品编码，只有一条有记录");
        }
		ieMaintenance.setPkId(BaseUtil.getUUID());
		ieMaintenance.setDelFlag(CommonConstants.DEL_FLAG);
		ieMaintenanceDao.insert(ieMaintenance);		
	}
	@Override
	public PageInfo<IeMaintenance> listIeMaintenanceByFilter(String equipCode, String productCode, Filter filter) {
		List<IeMaintenance> realList = new ArrayList<>();
        List<IeMaintenance> list =
                super.beforeList(IeMaintenanceDao.class, filter).listIeMaintenanceByFilter(equipCode,productCode);
        if(!list.isEmpty()) {
            for(IeMaintenance IeMaintenance :list) {
            	TmMaterial tmMaterial = ieMaintenanceDao.getProductUnitbyPkId(IeMaintenance.getProductCode());
            	String unit = tmMaterial.getMaterialUnit();
                String unitReal = "";
            	if(StringUtils.isNotEmpty(unit)){
                    String [] units = unit.split("/");
                    if (units.length>0){
                        unitReal = units[0];
                    }
                }
                if(StringUtils.isNotEmpty(IeMaintenance.getCapacity())){
                    IeMaintenance.setCapacity(IeMaintenance.getCapacity()+" "+unitReal+"/分");
                }
            	realList.add(IeMaintenance);
            }	
        }
		return  new PageInfo<IeMaintenance>(list);
	}
	@Override
	public IeMaintenance getIeMaintenanceById(String pkId) {
		return ieMaintenanceDao.selectById(pkId);
	}
	@Override
	public void updateIEMaintenance(IeMaintenance ieMaintenance, String loginName) {
        ieMaintenance.setDelFlag(CommonConstants.DEL_FLAG);
        EntityWrapper<IeMaintenance> ew = new EntityWrapper();
        ew.eq("EQUIP_CODE",ieMaintenance.getEquipCode()).eq("PRODUCT_CODE",ieMaintenance.getProductCode()).eq("DEL_FLAG",CommonConstants.DEL_FLAG).ne("PK_ID",ieMaintenance.getPkId());
        List<IeMaintenance> list = ieMaintenanceDao.selectList(ew);
        if(!list.isEmpty()){
            throw new ServiceException("同一个设备，同一产品编码，只有一条有记录");
        }
		ieMaintenanceDao.updateById(ieMaintenance);
	}
	@Override
	public void delIeMaintenanceById(String pkIds) {
        List<String> idList = new ArrayList<String>();
        String[] id = pkIds.split(",");
        for (int i = 0; i < id.length; i++) {
            idList.add(id[i]);
        }
		ieMaintenanceDao.deleteBatchIds(idList);
	}

}
